<?php  
/** 
 * Created by PhpStorm. 
 * User: raid 
 * Date: 2016/8/2 
 * Time: 11:03 
 */  
use Workerman\Worker;  
require_once './Autoloader.php';  

$worker = new Worker("websocket://0.0.0.0:8484"); 

$worker->count = 4;
 
$worker->uidConnections = array();
$worker->onMessage = function($connection, $buffer)
{
    // 向客户端发送hello 
    global $worker;
    if(!isset($connection->roomId))
    {
        $connection->roomId = $buffer;
        $connection->startime = time();
        $worker->uidConnections[$connection->roomId][] = $connection;
        
        $conn = reset($worker->uidConnections[$connection->roomId]);
        $conn->endtime = empty($conn->endtime) ? $connection->startime : $conn->endtime;
        $offlineTime = $connection->startime - $conn->endtime;
        //echo "[$connection->roomId]用户，离线时间：".$offlineTime."s\r\n";
        // 建立MySQL链接
        try{
            //$worker->db = new Workerman\Lib\Db('127.0.0.1', '3306', 'root', 'zzgame@)!*2018', 'pwzw_game', true);
            $worker->db = new Workerman\Lib\Db('111.230.187.245', '61955', 'root', 'oRxAc5Jk6_pHjuvIyaFhj1w9C3Zg%', 'pwzw_game', true);   
        }catch(Exception $e){
            var_dump($e);
        }

    //    if($connection->roomId == 'oRxAc5Jk6_pHjuvIyaFhj1w9C3Zg') {
            //连接Redis缓存连接
            //if( $offlineTime > 0 ) {
                
                // $redis = new \Redis();
                // $redis->connect('127.0.0.1',6379);       
                // $redis->auth('oRxAc5Jk6_pHjuvIyaFhj1w9C3Zg');
                // $redis->select(1);

                // $userKey = 'offline:user:'.$connection->roomId;
                // $redis->set($userKey,$offlineTime); // 离线时间差
                // $redis->expireAt($userKey,strtotime(date('Y-m-d 23:59:59'))); // 每日23点59分过期
            //}
    //    }
        if($offlineTime > 0) {
            $sql = "UPDATE `c_users` SET `offline_time` = offline_time + $offlineTime WHERE userid = '$connection->roomId'";
            $worker->db->query($sql);
        }
        
        
    }
};

// 当有客户端连接断开时
$worker->onClose = function($connection)
{
    global $worker;
    if(isset($connection->roomId))
    {
        if(isset($worker->uidConnections[$connection->roomId])){
            $conn = end($worker->uidConnections[$connection->roomId]);
            $conn->endtime = time();
            /*$gameTime = $conn->endtime - $conn->startime;
            echo ">>>>>>>>>>>>\r\n";
            echo "[$connection->roomId]用户，游戏时间：".$gameTime."s\r\n";
            
            $sql = "UPDATE `c_users` SET `game_time` = game_time + $gameTime WHERE userid = '$connection->roomId'";
            $worker->db->query($sql);*/
            
            // 连接数等于2则删除
            $count = count($worker->uidConnections[$connection->roomId]);
            if($count > 1){
                //unset($worker->uidConnections[$connection->roomId][0]);
                array_shift($worker->uidConnections[$connection->roomId]);
            }
        }
    }
};

// 运行worker
Worker::runAll();
